package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.consts.ErmsCodePrefix;
import com.sugon.dao.DevMainBaseAmDao;
import com.sugon.dao.DevTypeDao;
import com.sugon.dao.FactoryTypeDao;
import com.sugon.entity.DevMainBaseAmEntity;
import com.sugon.entity.DevTypeEntity;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.service.DevMainBaseAmService;
import com.sugon.utils.Constant;
import com.sugon.utils.StringUtils;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 设备自主维护库Service实现类
 *
 * @author YuXD
 * @date 2023-01-24
 */
@Service("devMainBaseAmService")
public class DevMainBaseAmServiceImpl implements DevMainBaseAmService {

    @Autowired
    private DevMainBaseAmDao devMainBaseAmDao;
    @Autowired
    private DevTypeDao devTypeDao;
    @Autowired
    private FactoryTypeDao factoryTypeDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevMainBaseAmEntity queryObject(String id) {
        return devMainBaseAmDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevMainBaseAmEntity> queryList(Map<String, Object> map) {
        return devMainBaseAmDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devMainBaseAmDao.queryTotal(map);
    }

    /**
     * queryAllList
     *
     * @param map
     * @return
     */
    @Override
    public List<DevMainBaseAmEntity> queryAllList(Map<String, Object> map) {
        return devMainBaseAmDao.queryAllList(map);
    }

    /**
     * queryAllByDevTotal
     *
     * @param map
     * @return
     */
    @Override
    public int queryAllByDevTotal(Map<String, Object> map) {
        return devMainBaseAmDao.queryAllByDevTotal(map);
    }

    /**
     * queryAllByDevList
     *
     * @param map
     * @return
     */
    @Override
    public List<DevMainBaseAmEntity> queryAllByDevList(Map<String, Object> map) {
        return devMainBaseAmDao.queryAllByDevList(map);
    }

    /**
     * 预防性
     * importPM
     *
     * @param file
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int importAM(MultipartFile file) {
        //获取所有表格数据
        List<String[]> list = ExcelImport.getExcelData(file);
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            DevMainBaseAmEntity dmb = null;
            boolean isSave = true;
            String[] array1 = list.get(i);
            String[] array = new String[14];
            System.arraycopy(array1, 0, array, 0, array1.length);
            // 根据选项编码是否填写确定逻辑
            String newDcode = array[0];
            if (StrUtil.isNotEmpty(newDcode)) {
                dmb = devMainBaseAmDao.getByDcode(newDcode);
                if (dmb != null) {
                    isSave = false;
                }
            }
            if (dmb == null) {
                dmb = new DevMainBaseAmEntity();
            }
            //选项名称
            if (StringUtils.isNotEmpty(array[1])) {
                dmb.setName(array[1]);
            }
            //设备类型
            if (StringUtils.isNotEmpty(array[2])) {
                String type = StrUtil.trim(array[2]);
                DevTypeEntity dt = devTypeDao.queryObjectByDCODE(type);
                if (dt == null) {
                    continue;
                }
                // 只能关联设备小类
                if (!NumberUtil.equals(dt.getTypeTp(), 0)) {
                    continue;
                }
                dmb.setDevType(type);
                dmb.setDevTypeName(dt.getTypeName());
            }
            //设备供应商
            if (StringUtils.isNotEmpty(array[3])) {
                String supply = StrUtil.trim(array[3]);
                List<FactoryTypeEntity> lists = factoryTypeDao.queryObjectByName(supply, 0);
                if (CollectionUtil.isEmpty(lists)) {
                    continue;
                }
                FactoryTypeEntity ft = lists.get(0);
                String code = ft.getFaCode();
                dmb.setDevSupply(code);
            }
            // 维护内容
            if (StringUtils.isNotEmpty(array[4])) {
                dmb.setCheckContent(StrUtil.trim(array[4]));
            }

            //维护标准
            if (StringUtils.isNotEmpty(array[5])) {
                dmb.setCheckSt(StrUtil.trim(array[5]));
            }
            //属性
            if (StringUtils.isNotEmpty(array[6])) {
                int arr = 1;
                switch (array[6]) {
                    case "C":
                        arr = 1;
                        break;
                    case "E":
                        arr = 2;
                        break;
                    case "S":
                        arr = 3;
                    default:
                        arr = 4;
                }
                dmb.setAttribute(arr);
            }
            // 物料需求
            if (StringUtils.isNotEmpty(array[7])) {
                dmb.setMaterialRequ(StrUtil.trim(array[7]));
            }

            // 方法及工具
            if (StringUtils.isNotEmpty(array[8])) {
                dmb.setMethodType(StrUtil.trim(array[8]));
            }
            // 备注
            if (StringUtils.isNotEmpty(array[9])) {
                dmb.setRemark(array[9]);
            }
            //人次
            if (StringUtils.isNotEmpty(array[10])) {
                dmb.setPerEv(Integer.parseInt(array[10]));
            }
            //生产时间
            if (StringUtils.isNotEmpty(array[11])) {
                dmb.setCheckDatePrud(Integer.parseInt(array[11]));
                dmb.setDevStatus(1);
            }
            //非生产时间
            if (StringUtils.isNotEmpty(array[12])) {
                dmb.setCheckDateUprud(Integer.parseInt(array[12]));
                dmb.setDevStatus(0);
            }
            //实施频率
            if (StringUtils.isNotEmpty(array[13])) {
                dmb.setFreRate(Integer.parseInt(array[13]));
            }
            if (isSave) {
                save(dmb);
            } else {
                update(dmb);
            }
            count++;
        }

        return count;
    }

    /**
     * queryAllTotal
     *
     * @param map
     * @return
     */
    @Override
    public int queryAllTotal(Map<String, Object> map) {
        return devMainBaseAmDao.queryAllTotal(map);
    }

    /**
     * save
     *
     * @param devMainBase 实体
     * @return
     */
    @Override
    public int save(DevMainBaseAmEntity devMainBase) {
        String baseId = RandomUtil.randomString(32);
        devMainBase.setId(baseId);
        devMainBase.setCreateUser(getUserId());
        devMainBase.setCreateDate(DateUtil.date());
        devMainBase.setUpdateDate(devMainBase.getCreateDate());
        devMainBase.setUpdateUser(devMainBase.getCreateUser());
        devMainBase.setLockFlag(1);
        devMainBase.setDelFlag(Constant.IS_NOT_DEL_INT);
        devMainBase.setCode(generateUniqueCode(null));
        // 给生产时间和非生产时间设置默认值
        if (devMainBase.getCheckDatePrud() == null) {
            devMainBase.setCheckDatePrud(0);
        }
        if (devMainBase.getCheckDateUprud() == null) {
            devMainBase.setCheckDateUprud(0);
        }
        return devMainBaseAmDao.save(devMainBase);
    }

    /**
     * generateUniqueCode
     *
     * @param object
     * @return
     */
    @Override
    public String generateUniqueCode(Object object) {
        // 声明当天的入库编码前缀
        String codePrefix = ErmsCodePrefix.AM_PREFIX;
        String code = (String) object;
        if (StrUtil.isEmpty(code)) {
            // 查询当前最新的入库编码
            code = devMainBaseAmDao.getLastDCode(codePrefix);
        }
        // 生成新的唯一编码
        code = com.sugon.modules.utils.StrUtil.increase(code, codePrefix, 4, 1);
        // 确保最新生成备件编码不重复
        DevMainBaseAmEntity amItem = queryByCode(code);
        if (amItem == null) {
            return code;
        }
        return generateUniqueCode(code);

    }

    @Override
    public DevMainBaseAmEntity queryByCode(String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        List<DevMainBaseAmEntity> amItemList = devMainBaseAmDao.queryList(param);
        return CollectionUtil.isNotEmpty(amItemList) ? amItemList.get(0) : null;
    }

    @Override
    public List<DevMainBaseAmEntity> getBySupplyAndType(String devSupply, String devType) {
        Map<String, Object> params = new HashMap<>();
        params.put("devSupply", devSupply);
        params.put("devType", devType);
        return devMainBaseAmDao.queryAllList(params);
    }

    /**
     * update
     *
     * @param devMainBase 实体
     * @return
     */
    @Override
    public int update(DevMainBaseAmEntity devMainBase) {
        devMainBase.setUpdateUser(getUserId());
        devMainBase.setUpdateDate(DateUtil.date());
        // 给生产时间和非生产时间设置默认值
        if (devMainBase.getCheckDatePrud() == null) {
            devMainBase.setCheckDatePrud(0);
        }
        if (devMainBase.getCheckDateUprud() == null) {
            devMainBase.setCheckDateUprud(0);
        }
        return devMainBaseAmDao.update(devMainBase);
    }

    /**
     * switchStatus
     *
     * @param on
     * @param ids
     */
    @Override
    public void switchStatus(Boolean on, String[] ids) {
        for (String id : ids) {
            DevMainBaseAmEntity devMainBase = devMainBaseAmDao.queryObject(id);
            if (on) {
                devMainBase.setLockFlag(1);
            } else {
                devMainBase.setLockFlag(0);
            }

            update(devMainBase);
        }
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devMainBaseAmDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devMainBaseAmDao.deleteBatch(ids);
    }

}
